<template>
  <basic-container>
    <chc-datagrid :dataTableId="dataTableId" :option="option" :page="page">
            <!-- <template slot="username"
                      slot-scope="scope">
              <span>hello-{{scope.row.username}}</span>
            </template> -->
            <template slot="menu"
                      slot-scope="scope">
              <el-button size="small"
                         type="text"
                         icon="el-icon-plus"
                         @click="handleMessageBodyOpen(scope.row,scope.index)">消息内容
              </el-button>
            </template>
    </chc-datagrid>
    <el-dialog title="消息内容" :visible.sync="messageBody.dlgVisible" width="60%">
        <el-form  label-width="0px">
          <el-input type="textarea" v-model="messageBody.data.content" rows="20"
              :readonly="!(permissions['edi.messageIn.edit'])" auto-complete="off"></el-input>
        </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="permissions['edi.messageIn.edit']" type="primary"
                    @click="handleMessageBodySave()">保存</el-button>
        <el-button v-if="permissions['edi.messageIn.edit']" @click="handleMessageBodyClose()">取消</el-button>
        <el-button v-if="!permissions['edi.messageIn.edit']" @click="handleMessageBodyClose()">关闭</el-button>
      </div>

    </el-dialog>
  </basic-container>


</template>

<script>
  import {addData, deleteData,updateData, queryData, Operator,getAVueOptionCode} from "@/components/chc-ui/api/data-table"
  import {mapGetters} from "vuex";
  import tableOption from "./tableOption"
  export default {
    name: 'messageIn',
     data() {
      return {
        option: tableOption,
        page: {
          pageSize: 20, // 每页显示多少条,
        },
        dataTableId : 'edi.messageIn',
        messageInId: '',
        messageBody:{
          dlgVisible: false,
          data: {
            id: '',
            content:''
          }

        }
      };
    },
    computed:{
      ...mapGetters(["permissions"])
    },
    methods:{
      handleMessageBodyOpen(row,index){
        this.messageInId = row.id;
        queryData('edi.messageInBody', {
            current: 1,
            size: 1,
            //columns: columns,
            //env: {},
            // isSummary: false,
            conditions: [{
              field: 'messageInId',
              operator: '=',
              value: row.id
            }]
            // orderBys: orderBys
          }).then(response => {
            if (response.data.data.records && response.data.data.records.length>0){
              this.$set(this.messageBody,"data",response.data.data.records[0]);
            }
            this.messageBody.dlgVisible=true;
          });
      },
      handleMessageBodySave(){
        if (this.messageBody.data.messageInId)
          this.updateMessageBody();
        else
          this.addMessageBody();

      },
      handleMessageBodyClose(){
        this.messageBody.dlgVisible=false;
      },
      addMessageBody(){
        var params = {};
        this.messageBody.data.messageInId = this.messageInId;
        params.data = this.messageBody.data;
        addData("edi.messageInBody", params).then((response) => {
          this.messageBody.data= response.data.data;
          //this.getList(this.page);
          this.$notify({
            title: "成功",
            message: "保存消息内容成功",
            type: "success",
            duration: 2000
          });
          this.messageBody.dlgVisible=false;
        }).catch((error) => {
          console.log(error);
          this.$notify({
            title: "失败",
            message: "保存消息内容失败: " + error,
            type: "error",
            duration: 2000
          });
        });

      },
      updateMessageBody(){
        var params = {};
        params.data = this.messageBody.data;
        updateData("edi.messageInBody", params).then((response) => {
          this.messageBody.data= response.data.data;
          //this.getList(this.page);
          this.$notify({
            title: "成功",
            message: "保存消息内容成功",
            type: "success",
            duration: 2000
          });
          this.messageBody.dlgVisible=false;
        }).catch((error) => {
          console.log(error);
          this.$notify({
            title: "失败",
            message: "保存消息内容失败: " + error,
            type: "error",
            duration: 2000
          });
        });


      }

    }
  };
</script>
